import { createRouter, createWebHistory } from 'vue-router'
// import { h } from 'vue'
import Layout from '../layouts/LayOutColum.vue' // Layout布局

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: '登录页',
      component: () => import('../views/AppLogin.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '',
      name: '',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        // 评估展示
        {
          path: '/QualityAssessment',
          name: '评估展示',
          children: [
            {
              path: '/QualityAssessment',
              name: '质量评估',
              component: () => import('../views/AssessmentShowcase/QualityAssessment.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/TimelinessAssessment',
              name: '时效性评估',
              component: () => import('../views/AssessmentShowcase/TimelinessAssessment.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/StabilityAssessment',
              name: '稳定性评估',
              component: () => import('../views/AssessmentShowcase/StabilityAssessment.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/ComplexTerrainAssessment',
              name: '复杂地形评估',
              component: () => import('../views/AssessmentShowcase/ComplexTerrainAssessment.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/WeatherProcessAssessment',
              name: '天气过程评估',
              component: () => import('../views/AssessmentShowcase/WeatherProcessAssessment.vue'),
              meta: { requiresAuth: true },
            },
          ],
        },
        // 评估报告
        {
          path: '/WeekReports',
          name: '评估报告',
          meta: { requiresAuth: true },
          children: [
            {
              path: '/WeekReports',
              name: '周报',
              component: () => import('../views/AssessmentReports/WeekReports.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/MonthReports',
              name: '月报',
              component: () => import('../views/AssessmentReports/MonthReports.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/WeatherProcessReport',
              name: '天气过程报告',
              component: () => import('../views/AssessmentReports/WeatherProcessReport.vue'),
              meta: { requiresAuth: true },
            },
          ],
        },
        // 相关文档
        {
          path: '/AssessmentNorms',
          name: '相关文档',
          meta: { requiresAuth: true },
          children: [
            {
              path: '/AssessmentNorms',
              name: '评估标准',
              component: () => import('../views/RelatedDocuments/AssessmentNorms.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/AssessmentRules',
              name: '评估细则',
              component: () => import('../views/RelatedDocuments/AssessmentRules.vue'),
              meta: { requiresAuth: true },
            },
          ],
        },
        // 评估配置
        {
          path: '/BasicConfiguration',
          name: '评估配置',
          meta: { requiresAuth: true },
          children: [
            {
              path: '/BasicConfiguration',
              name: '基础配置',
              component: () => import('../views/AssessmentConfiguration/BasicConfiguration.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/TaskConfiguration',
              name: '任务配置',
              component: () => import('../views/AssessmentConfiguration/TaskConfiguration.vue'),
              meta: { requiresAuth: true },
            },
          ],
        },
        {
          path: '/UserManagement',
          name: '系统管理',
          meta: { requiresAuth: true },
          children: [
            {
              path: '/UserManagement',
              name: '用户管理',
              component: () => import('../views/SystemManagement/UserManagement.vue'),
              meta: { requiresAuth: true },
            },
            {
              path: '/LogManagement',
              name: '日志管理',
              component: () => import('../views/SystemManagement/LogManagement.vue'),
              meta: { requiresAuth: true },
            },
          ],
        },
      ],
    },
  ],
})

// 设置默认路由
router.beforeEach((to, from, next) => {
  //定义登录状态
  let isAuthenticated = false
  //判断本地存储的token是否存在
  if (localStorage.getItem('token')) {
    // 存在则登录状态为true
    isAuthenticated = true
  }
  //设置默认路由
  if (to.path === '' || to.path === '/') {
    next('/login')
  } else {
    //如果需要登录且未登录，跳转登录页
    if (to.meta.requiresAuth && !isAuthenticated) {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
